package com.ny.view;
import com.ny.dao.StudentDao;
import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.entity.Student;
import com.ny.service.OrderService;
import com.ny.service.ProductService;
import com.ny.service.StudentService;
import com.ny.service.impl.OrderServiceImpl;
import com.ny.service.impl.ProductServiceImpl;
import com.ny.service.impl.StudentServiceImpl;

import java.util.List;
import java.util.Scanner;
public class IndexView {
    private static StudentService studentService = new StudentServiceImpl();
    private static ProductService productService = new ProductServiceImpl();
    static OrderService orderService = new OrderServiceImpl();

    public static void main(String[] args) throws Exception {
        boolean flag = true;
        while (flag) {
            System.out.println("欢迎来到校园商品系统");
            System.out.println("请选择");
            System.out.println("1.用户登录");
            System.out.println("2.注册");
            System.out.println("3.退出");
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            switch (n) {
                case 1:
                    System.out.println("请登录");
                    System.out.println("请输入用户名");
                    String username = scanner.next();
                    System.out.println("请输入密码");
                    String password = scanner.next();
                    Student student = studentService.login(username, password);
                    if (student != null) {
                        System.out.println("登录成功:来到商品显示界面");
                        List<Product> productList = productService.findAllProduct();

                        System.out.print("商品序号\t\t");
                        System.out.print("商品名称\t\t");
                        System.out.print("商品库存\t\t");
                        System.out.print("商品价格\t\t");
                        System.out.print("商品种类\t\t");
                        System.out.println("商品介绍\t\t");
                        for (Product product : productList) {
                            System.out.print(product.getProductId() + "\t\t");
                            System.out.print(product.getProductName() + "\t\t");
                            System.out.print(product.getStock() + "\t\t");
                            System.out.print(product.getPrice() + "\t\t");
                            System.out.print(product.getCategory() + "\t\t");
                            System.out.println(product.getDescription());
                        }

                        System.out.println("请选择商品序号");
                        int productId = scanner.nextInt();
                        System.out.println("请输入商品数量");
                        int num = scanner.nextInt();
                        Order order = new Order();
                        order.setStudentId(student.getStudentID());
                        order.setProductId(productId);
                        order.setNumber(num);
                        order.setPrice(productList.get(productId - 1).getPrice() * num);
                        int i = orderService.insertOrder(order);
                        if (i > 0) {
                            System.out.println("添加成功");
                            productService.updateProductStock(productId, num);
                            System.out.println("库存减少成功");

                        }
                    } else {
                        System.out.println("重新登录");
                    }
                    break;
                case 2:
                    System.out.println("请注册");
                    Student s = new Student();
                    System.out.println("请输入用户名");
                    String name = scanner.next();
                    System.out.println("请输入密码");
                    String pwd = scanner.next();
                    System.out.println("请输入手机号");
                    String phone = scanner.next();
                    s.setName(name);
                    s.setPassword(pwd);
                    s.setPhone(phone);
                    int i = studentService.insertStudent(s);
                    if (i > 0) {
                        System.out.println("注册成功");
                    } else {
                        System.out.println("注册失败");
                    }
                    break;
                case 3:
                    System.out.println("退出系统");
                    flag = false;
                    break;
                default:
                    System.out.println("请输入正确的选择序号");
            }
        }
    }
}